package com.yanchangapp.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yanchangapp.R;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.util.window.KeyboardHelper;

/**
 * Created by apple on 2019-09-12.
 * description: 供应商和数字的dialog
 */
public class SupplierNumDialog extends Dialog {
    private Context context;
    public SupplierNumDialog(@NonNull Context context) {
        this(context,0);
    }

    public SupplierNumDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public interface SpEnsureClickListener{
        void ensureClick(int num);
    }

    public interface SpJumpSuppliersListener{
        void jumpSupplier(String numStr);
    }

    @Override
    public void dismiss() {
        InputMethodManager imm = (InputMethodManager) context.getApplicationContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        // 即使当前焦点不在editText，也是可以隐藏的。
        if (imm != null) {
            final View currentFocus = getCurrentFocus();
            IBinder windowToken = null;
            if (currentFocus != null) {
                windowToken = currentFocus.getWindowToken();
            }
            if (windowToken != null) {
                imm.hideSoftInputFromWindow(windowToken, 0);
            }
        }
        super.dismiss();
    }

    public static class Builder{
        private Context mContext;
        private int minRange,maxRange;
        private String tipString,supplierName,buyNumStr;
        private SpEnsureClickListener mSpEnsureClickListener;
        private SpJumpSuppliersListener mSpJumpSuppliersListener;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setBuyRange(int minRange,int maxRange){
            this.minRange = minRange;
            this.maxRange = maxRange;
            return this;
        }

        public Builder setTipsString(String tipsString){
            this.tipString = tipsString;
            return this;
        }

        public Builder setBuyNum(String buyNumStr){
            this.buyNumStr =buyNumStr;
            return this;
        }

        public Builder setSupplierName(String supplierName){
            this.supplierName = supplierName;
            return this;
        }

        public Builder setEnsureListener(SpEnsureClickListener spEnsureClickListener){
            this.mSpEnsureClickListener = spEnsureClickListener;
            return this;
        }

        public Builder setJumpSupplierListener(SpJumpSuppliersListener supplierListener){
            this.mSpJumpSuppliersListener = supplierListener;
            return this;
        }

        public SupplierNumDialog build(){
            final SupplierNumDialog supplierNumDialog = new SupplierNumDialog(mContext,R.style.BottomDialog);
            View contentView = LayoutInflater.from(mContext).inflate(R.layout.bottom_apply_first, null, false);
            // 设置内部的布局
            TextView tvSupplierName = contentView.findViewById(R.id.supplierName);
            EditText etBuyNum = contentView.findViewById(R.id.buyerNum);
            TextView tvTips = contentView.findViewById(R.id.tvTips);
            tvTips.setText(tipString);
            tvTips.setVisibility(View.GONE);
            etBuyNum.setText(buyNumStr);
            if (!TextUtils.isEmpty(buyNumStr)){
                etBuyNum.setSelection(buyNumStr.length());
            }
            tvSupplierName.setText(supplierName);

            etBuyNum.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (tvTips.getVisibility() == View.VISIBLE) {
                        tvTips.setVisibility(View.GONE);
                    }
                }
            });

            // 确认按钮点击事件...
            contentView.findViewById(R.id.btnEnsure).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String text = etBuyNum.getText().toString();
                    if (!TextUtils.isEmpty(text)) {
                        int innerNum = Integer.parseInt(text);
                        if (innerNum == 0) {
                            UIUtils.showToast("采购数量不能为0");
                            return;
                        }
                        if (innerNum <= maxRange && innerNum >= minRange) {
                            if (mSpEnsureClickListener != null){
                                mSpEnsureClickListener.ensureClick(innerNum);
                            }
                        } else {
                            tvTips.setVisibility(View.VISIBLE);
                        }
                    } else {
                        UIUtils.showToast("请输入采购数量");
                    }
                }
            });

            // 供应商点击选择点击事件
            contentView.findViewById(R.id.rlSupplier).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String text = etBuyNum.getText().toString();
                    supplierNumDialog.dismiss();
                    if (mSpJumpSuppliersListener != null){
                        mSpJumpSuppliersListener.jumpSupplier(text);
                    }
                }
            });

            // 关闭点击事件
            contentView.findViewById(R.id.closeBottom).setOnClickListener(v -> supplierNumDialog.dismiss());


            Window window = supplierNumDialog.getWindow();
            if (window != null){
                window.getDecorView().setPadding(0,0,0,0);
                window.setGravity(Gravity.BOTTOM);
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(attributes);
            }
            supplierNumDialog.setContentView(contentView);
            return supplierNumDialog;
        }
    }
}
